package com.twu.biblioteca.service;

import com.twu.biblioteca.model.Library;
import com.twu.biblioteca.model.LibraryService;
import com.twu.biblioteca.model.Movie;
import com.twu.biblioteca.model.ServiceResult;

import java.util.Map;

/**
 * Created by davecroman on 19/09/2014.
 */
public class ListMoviesService extends LibraryService{

    public ListMoviesService(Library library){
        super(library);
    }

    /**
     * Displays all movies that are available for borrowing.
     */
    @Override
    public ServiceResult performService() {
        final Map<Integer, Movie> allMovies = library.getAllMovies();
        StringBuilder result = new StringBuilder();

        result.append("List of Available Movies:\n");
        result.append("===========================================================================\n");
        result.append(String.format("%s| %-25s | %-25s | %-4s | %-7s\n", "ID", "Title", "Director", "Year", "Rating"));
        result.append("===========================================================================\n");
        for(Movie movie : allMovies.values()){
            if( movie.isAvailable() ) {
                result.append(movie + "\n");
            }
        }

        return new ServiceResult(result.toString());
    }

    @Override
    public String getName() {
        return "List Movies";
    }
}
